package com.ljh.mq.springrabbitmqdemo.controller;

import com.ljh.mq.springrabbitmqdemo.constants.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: Themberfue
 * @date: 2025/4/28 20:37
 * @description:
 */
@RequestMapping("/producer")
@RestController
public class ProducerController {
    private static final Logger log = LoggerFactory.getLogger(ProducerController.class);

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("/work")
    public String work() {
        for (int i = 0; i < 10; i++) {
            String msg = "hello work queue mode~ " + i;
            // ? 当使用默认交换机时，routingKey 和队列名称保持一致
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE, msg);
        }
        log.info("消息发送成功");
        return "消息发送成功";
    }

    @RequestMapping("/fanout")
    public String fanout() {
        for (int i = 0; i < 10; i++) {
            String msg = "hello publish fanout mode~ " + i;
            // ? 当使用默认交换机时，routingKey 和队列名称保持一致
            rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE, "", msg);
        }
        log.info("消息发送成功");
        return "消息发送成功";
    }

    @RequestMapping("/direct/{routingKey}")
    public String direct(@PathVariable("routingKey") String routingKey) {
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, routingKey, "hello routing mode~；routingKey is " + routingKey);
        log.info("消息发送成功：{}", routingKey);
        return "消息发送成功：" + routingKey;
    }

    @RequestMapping("/topic/{routingKey}")
    public String topic(@PathVariable("routingKey") String routingKey) {
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, routingKey, "hello topic mode~；routingKey is " + routingKey);
        log.info("消息发送成功：{}", routingKey);
        return "消息发送成功：" + routingKey;
    }
}
